package com.itlong.cloud.controller.sharedELevator;

import com.itlong.cloud.POJO.DTO.property.PropertyMealManageGetDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.property.PropertyMealManageGetVO;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.PropertyErrorCodeEnum;
import com.itlong.cloud.paymenthub.IPropertyMealManageServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * <desc>
 *      共享电梯套餐管理Controller
 * </desc>
 *
 * @createDate 2020/07/15
 */
@RestController
@RequestMapping("/sharedElevator/mealManage")
public class PropertyMealManageController {

    private static final Logger LOG = LoggerFactory.getLogger(PropertyMealManageController.class);

    @Autowired
    IPropertyMealManageServiceClient iPropertyMealManageServiceClient;

    /**
     * <desc>
     *     共享电梯套餐列表
     * <desc/>
     *
     *@param  propertyMealManageGetDTO  分页查询共享电梯套餐
     * @return  MessageVO 标准格式化响应结果
     * @author Qiang.S
     * @createDate 2020/07/15
     */
    @ResponseBody
    @PostMapping(path = "/getMealManageByPage")
    public Object getMealManageByPage(PropertyMealManageGetDTO propertyMealManageGetDTO) throws Exception {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyMealManageGetDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyMealManageGetDTO.class, request);
            if (propertyMealManageGetDTO == null) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }

            Page<PropertyMealManageGetVO> result = iPropertyMealManageServiceClient.getMealManageByPage(propertyMealManageGetDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), result);
        } catch (Exception e) {
            throw new DataAccessException("【物业_共享电梯套餐列表】分页查询失败", e);
        }
    }

    /**
     * <desc>
     *     添加共享电梯套餐
     * <desc/>
     *
     *@param  propertyMealManageGetDTO  共享电梯套餐
     * @return  MessageVO 标准格式化响应结果
     * @author Qiang.S
     * @createDate 2020/07/15
     */
    @ResponseBody
    @AuthAccessRequired
    @PostMapping(path = "/saveMealManageInfo")
    @LoggerInfo(operateDesc = LogDescConstant.PROPERTY_MEAL_MANAGE_SAVE_LOG_DESC)
    public Object saveMealManageInfo(PropertyMealManageGetDTO propertyMealManageGetDTO) throws Exception {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyMealManageGetDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyMealManageGetDTO.class, request);
            if (propertyMealManageGetDTO == null) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            iPropertyMealManageServiceClient.saveMealManageInfo(propertyMealManageGetDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【物业_共享电梯套餐列表】添加失败", e);
        }
    }

    /**
     * <desc>
     *     查看共享电梯套餐
     * <desc/>
     *
     *@param  propertyMealManageGetDTO  共享电梯套餐
     * @return  MessageVO 标准格式化响应结果
     * @author Qiang.S
     * @createDate 2020/07/15
     */
    @ResponseBody
    @PostMapping(path = "/getMealManageDetail")
    @LoggerInfo(operateDesc = LogDescConstant.PROPERTY_MEAL_MANAGE_UPDATE_LOG_DESC)
    public Object getMealManageDetail(PropertyMealManageGetDTO propertyMealManageGetDTO) throws Exception {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyMealManageGetDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyMealManageGetDTO.class, request);
            if (propertyMealManageGetDTO == null || StringUtils.isBlank(propertyMealManageGetDTO.getMealId())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iPropertyMealManageServiceClient.getMealManageDetail(propertyMealManageGetDTO.getMealId()));
        } catch (Exception e) {
            throw new DataAccessException("【物业_共享电梯套餐列表】更新失败", e);
        }
    }

    /**
     * <desc>
     *     更新共享电梯套餐
     * <desc/>
     *
     *@param  propertyMealManageGetDTO  共享电梯套餐
     * @return  MessageVO 标准格式化响应结果
     * @author Qiang.S
     * @createDate 2020/07/15
     */
    @ResponseBody
    @AuthAccessRequired
    @PostMapping(path = "/updateMealManageInfo")
    @LoggerInfo(operateDesc = LogDescConstant.PROPERTY_MEAL_MANAGE_UPDATE_LOG_DESC)
    public Object updateMealManageInfo(PropertyMealManageGetDTO propertyMealManageGetDTO) throws Exception {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyMealManageGetDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyMealManageGetDTO.class, request);
            if (propertyMealManageGetDTO == null || StringUtils.isBlank(propertyMealManageGetDTO.getMealId())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            iPropertyMealManageServiceClient.updateMealManageInfo(propertyMealManageGetDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【物业_共享电梯套餐列表】更新失败", e);
        }
    }

    /**
     * <desc>
     *     删除共享电梯套餐
     * <desc/>
     *
     *@param  propertyMealManageGetDTO  共享电梯套餐
     * @return  MessageVO 标准格式化响应结果
     * @author Qiang.S
     * @createDate 2020/07/15
     */
    @ResponseBody
    @AuthAccessRequired
    @PostMapping(path = "/deleteMealManageInfo")
    @LoggerInfo(operateDesc = LogDescConstant.PROPERTY_MEAL_MANAGE_DELETE_LOG_DESC)
    public Object deleteMealManageInfo(PropertyMealManageGetDTO propertyMealManageGetDTO) throws Exception {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyMealManageGetDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyMealManageGetDTO.class, request);
            if (propertyMealManageGetDTO == null || StringUtils.isBlank(propertyMealManageGetDTO.getMealId())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            if (iPropertyMealManageServiceClient.checkMeal(propertyMealManageGetDTO.getMealId()) > 0) {
                return new MessageVO(PropertyErrorCodeEnum.ELEVATOR_MEAL_IS_USE_ERR.getErrorCode());
            }
            iPropertyMealManageServiceClient.deleteMealManageInfo(propertyMealManageGetDTO.getMealId());
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【物业_共享电梯套餐列表】删除失败", e);
        }
    }

    /**
     * <desc>
     *     移动共享电梯套餐
     * <desc/>
     *
     *@param  propertyMealManageGetDTO  共享电梯套餐
     * @return  MessageVO 标准格式化响应结果
     * @author Qiang.S
     * @createDate 2020/07/15
     */
    @ResponseBody
    @AuthAccessRequired
    @PostMapping(path = "/moveMealManageInfo")
    @LoggerInfo(operateDesc = LogDescConstant.PROPERTY_MEAL_MANAGE_MOVE_LOG_DESC)
    public Object moveMealManageInfo(PropertyMealManageGetDTO propertyMealManageGetDTO) throws Exception {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            propertyMealManageGetDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyMealManageGetDTO.class, request);
            if (propertyMealManageGetDTO == null || StringUtils.isBlank(propertyMealManageGetDTO.getMoveMealId())
                    || StringUtils.isBlank(propertyMealManageGetDTO.getReplaceMealId()) || propertyMealManageGetDTO.getUpOrDown() == null) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            iPropertyMealManageServiceClient.moveMealManageInfo(propertyMealManageGetDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        } catch (Exception e) {
            throw new DataAccessException("【物业_共享电梯套餐列表】移动失败", e);
        }
    }
}